package com.java.main.service.token.impl;

import java.util.*;

//import com.hibaby.crm.domain.customer.CustomerUserDO;
//import AutoField;
//import SqlAssembleUtils;
//import org.apache.poi.hssf.record.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import KbmContent;
import com.java.main.domain.token.TokenDO;
import com.java.main.domain.user.UserDO;
import com.java.main.service.SysBaseServiceImpl;
import com.java.main.service.token.TokenService;
//import sun.tools.jstat.Token;

import javax.persistence.EntityManager;
//import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

@Service("tokenService")
public class TokenServiceImpl extends SysBaseServiceImpl<TokenDO, Integer> implements TokenService {
	@Value("${expiry_time}")
	private String expiryTime;

	@PersistenceContext
	EntityManager entityManager;

	@Override
	@Transactional
	public String getTokenByUser(UserDO user) {
		TokenDO entity = new TokenDO();// findByUserId(userId); 支持多点登录
		String token = UUID.randomUUID().toString().replaceAll("-", "");
		entity.setToken(token);
		entity.setUserId(user.getId());
		entity.setType(0);
		Long l = System.currentTimeMillis() + Long.parseLong(expiryTime);
		entity.setExpiryTime(new Date(l));
		add(entity);

		return entity.getToken();
	}
	// 刷新token有效期
	@Override
	@Transactional
	public void flushTokenDO(TokenDO tokenDO) {
		Long l = System.currentTimeMillis() + Long.parseLong(expiryTime);
		tokenDO.setExpiryTime(new Date(l));
		entityManager.merge(tokenDO);
		entityManager.flush();
	}

//	@Transactional
//	public String getTokenByCusetomerUser(UserDO user){
//		TokenDO entity = null;// findByUserId(userId); 支持多点登录
//		if (entity == null) {
//
//			entity = new TokenDO();
//			KbmContent content = new KbmContent();
//			content.setCurrentUserId(user.getId());
//			content.setEndemicId(-1);
//			//content.setCategoryId(user.getCategoryId());
//			entity.setKbmContent(content);
//			entity.setSoftDelete(0);
//			String token = UUID.randomUUID().toString().replaceAll("-", "");
//			entity.setToken(token);
//			entity.setUserId(user.getId());
//			entity.setIp("127.0.0.1");
//			entity.setType(0);
//			Long l = System.currentTimeMillis() + Long.parseLong(expiryTime);
//			entity.setExpiryTime(new Date(l));
//			add(entity);
//		}
//		return entity.getToken();
//	}
	@Override
	@Transactional
	public void delByUserId(Integer userId) {
		TokenDO entity = findByUserId(userId);
		del(entity);
	}

	@Override
	public TokenDO findByUserId(Integer userId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", userId);
		return findOne(param);
	}

	@Override
	public TokenDO findByToken(String token){
//		TokenDO tokenDO = entityManager.find(TokenDO.class, token);
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM token WHERE token = '"+token+"' ");
		List<TokenDO> list = (List<TokenDO>) entityManager.createNativeQuery(sql.toString(), TokenDO.class).getResultList();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	@Override
	@Transactional
		public void deleteTokenDObyToken(String token){
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM token WHERE token = '"+token+"' ");
		entityManager.createNativeQuery(sql.toString(), TokenDO.class).executeUpdate();
	}
}
